﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// RouteAdd 的摘要说明
    /// </summary>
    public class RouteAdd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string ROUTE_NAME = context.Request["ROUTE_NAME"];
                string ROUTE_DES = context.Request["ROUTE_DES"];
                string ROUTE_CHARACTERISTIC = context.Request["ROUTE_CHARACTERISTIC"];
                string ROUTE_PRICE = context.Request["ROUTE_PRICE"];
                string ROUTE_CHENGDU = context.Request["ROUTE_CHENGDU"];
                string ROUTE_BEIJING = context.Request["ROUTE_BEIJING"];
                string ROUTE_ELSECITY = context.Request["ROUTE_ELSECITY"];
                string ROUTE_DAYS = context.Request["ROUTE_DAYS"];
                string ROUTE_DATE_START = context.Request["ROUTE_DATE_START"];
                string ROUTE_DATE_END = context.Request["ROUTE_DATE_END"];
                string ROUTE_HOT = context.Request["ROUTE_HOT"];
                string ROUTE_ELABORATE = context.Request["ROUTE_ELABORATE"];
                string ROUTE_ORIGINAL = context.Request["ROUTE_ORIGINAL"];
                string ROUTE_BAK = context.Request["ROUTE_BAK"];

                string[] continents = string.IsNullOrEmpty(context.Request["continent[0][]"]) ? new string[] { } : context.Request["continent[0][]"].Split(',');
                string[] countrys = string.IsNullOrEmpty(context.Request["country[0][]"]) ? new string[] { } : context.Request["country[0][]"].Split(',');
                string[] citys = string.IsNullOrEmpty(context.Request["city[0][]"]) ? new string[] { } : context.Request["city[0][]"].Split(',');

                string[] characteristics = string.IsNullOrEmpty(context.Request["characteristic[0][]"]) ? new string[] { } : context.Request["characteristic[0][]"].Split(',');
                string[] themes = string.IsNullOrEmpty(context.Request["theme[0][]"]) ? new string[] { } : context.Request["theme[0][]"].Split(',');
                string[] hotels = string.IsNullOrEmpty(context.Request["hotel[0][]"]) ? new string[] { } : context.Request["hotel[0][]"].Split(',');
                string[] specialitems = string.IsNullOrEmpty(context.Request["specialitem[0][]"]) ? new string[] { } : context.Request["specialitem[0][]"].Split(',');
                string[] photos = string.IsNullOrEmpty(context.Request["photo[0][]"]) ? new string[] { } : context.Request["photo[0][]"].Split(',');
                string[] trips = string.IsNullOrEmpty(context.Request["trip[0][]"]) ? new string[] { } : context.Request["trip[0][]"].Split(',');


                Route_BLL routeBLL = new Route_BLL();
                ROUTE route = new ROUTE()
                {
                    ROUTE_ID = Util.GetUID(),
                    ROUTE_NAME = ROUTE_NAME,
                    ROUTE_DES = ROUTE_DES,
                    ROUTE_CHARACTERISTIC = ROUTE_CHARACTERISTIC,
                    ROUTE_PRICE = int.Parse(ROUTE_PRICE),
                    ROUTE_CHENGDU = int.Parse(ROUTE_CHENGDU),
                    ROUTE_BEIJING = int.Parse(ROUTE_BEIJING),
                    ROUTE_ELSECITY = ROUTE_ELSECITY,
                    ROUTE_DAYS = short.Parse(ROUTE_DAYS),
                    ROUTE_DATE_START = DateTime.Parse(ROUTE_DATE_START),
                    ROUTE_DATE_END = DateTime.Parse(ROUTE_DATE_END),
                    ROUTE_HOT = short.Parse(ROUTE_HOT),
                    ROUTE_ELABORATE = short.Parse(ROUTE_ELABORATE),
                    ROUTE_ORIGINAL = short.Parse(ROUTE_ORIGINAL),
                    ROUTE_BAK = ROUTE_BAK
                };

                //添加旅途城市
                CityBelong_BLL cbBLL = new CityBelong_BLL();
                for (int i = 0; i < citys.Length; i++)
                {
                    info = cbBLL.Add(new ROUTE_COUNTRY()
                    {
                        ROUTE_ID = route.ROUTE_ID,
                        ROUTE_CITY = citys[i],
                        ROUTE_CONTINENT = continents[i],
                        ROUTE_COUNTRY_NAME = countrys[i]
                    });

                    System.Threading.Thread.Sleep(123);
                }

                //添加酒店信息
                Route_Hotel_BLL rhBLL = new Route_Hotel_BLL();
                foreach (string h in hotels)
                {
                    rhBLL.Add(new ROUTE_HOTEL()
                    {
                        ROUTE_ID = route.ROUTE_ID,
                        HOTE_ID = h
                    });
                    System.Threading.Thread.Sleep(123);
                }
                //添加特色项目
                Route_SpecialItem_BLL rsBLL = new Route_SpecialItem_BLL();
                foreach (string item in specialitems)
                {
                    rsBLL.Add(new ROUTE_SPECIALITEM()
                    {
                        ROUTE_ID = route.ROUTE_ID,
                        SPECIALITEM_ID = item
                    });
                    System.Threading.Thread.Sleep(123);
                }

                //添加路线特色
                Route_Characteristic_BLL rcBLL = new Route_Characteristic_BLL();
                foreach (string c in characteristics)
                {
                    rcBLL.Add(new ROUTE_CHARACTERISTIC()
                    {
                        ROUTE_ID = route.ROUTE_ID,
                        CHARACTERISTIC_ID = c
                    });

                    System.Threading.Thread.Sleep(123);
                }
                //添加路线主题
                Route_Theme_BLL rtBLL = new Route_Theme_BLL();
                foreach (string theme in themes)
                {
                    rtBLL.Add(new ROUTE_THEME()
                    {
                        ROUTE_ID = route.ROUTE_ID,
                        THEME_ID = theme
                    });

                    System.Threading.Thread.Sleep(123);
                }
                //添加路线图片
                Route_Gallery_BLL rgBLL = new Route_Gallery_BLL();
                foreach (string photo in photos)
                {
                    rgBLL.Add(new ROUTE_GALLERY()
                    {
                        ROUTE_ID = route.ROUTE_ID,
                        PHOTO_ID = photo
                    });

                    System.Threading.Thread.Sleep(123);
                }

                //添加行程信息
                Route_Trip_BLL rtpBLL = new Route_Trip_BLL();
                foreach (string trip in trips)
                {
                    rtpBLL.Add(new ROUTE_TRIP()
                    {
                        ROUTE_ID = route.ROUTE_ID,
                        TRIP_ID = trip
                    });

                    System.Threading.Thread.Sleep(123);
                }

                info = routeBLL.Add(route);
                System.Threading.Thread.Sleep(123);
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("添加路线失败", ex);
                info.Success = false;
                info.Title = "错误";
                info.Tip = "<h5><p>添加路线失败</p></h5>";
                info.Url = "RouteAdd.aspx";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}